﻿using _11._24._2014_AngularPractice.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11._24._2014_AngularPractice.Data
{
    public static class Seeder
    {
        public static void Seed(ApplicationDbContext db)
        {
            UserManager<ApplicationUser> userManager =
                new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
            RoleManager<Role> roleManager =
                new RoleManager<Role>(new RoleStore<Role>(db));

            //Seed roles
            if (!roleManager.RoleExists("Admin"))
            {
                roleManager.Create(new Role
                {
                    Name = "Admin"
                });
            }

            if (!roleManager.RoleExists("General"))
            {
                roleManager.Create(new Role
                {
                    Name = "General"
                });
            }

            //Seed users
            ApplicationUser user1 = userManager.FindByEmail("nick@email.com");

            if (user1 == null)
            {
                userManager.Create(new ApplicationUser
                {
                    Email = "nick@email.com",
                    UserName = "nick@email.com",
                    FirstName = "Nick",
                    LastName = "Brittain"
                }, "123456");

                user1 = userManager.FindByEmail("nick@email.com");
            }

            if (!userManager.IsInRole(user1.Id, "Admin"))
            {
                userManager.AddToRole(user1.Id, "Admin");
            }

            ApplicationUser user2 = userManager.FindByEmail("mallory@email.com");

            if (user2 == null)
            {
                userManager.Create(new ApplicationUser
                {
                    Email = "mallory@email.com",
                    UserName = "mallory@email.com",
                    FirstName = "Mallory",
                    LastName = "Deskis"
                }, "123456");

                user2 = userManager.FindByEmail("mallory@email.com");
            }

            if (!userManager.IsInRole(user2.Id, "General"))
            {
                userManager.AddToRole(user2.Id, "General");
            }

            ApplicationUser user3 = userManager.FindByEmail("dev@email.com");

            if (user3 == null)
            {
                userManager.Create(new ApplicationUser
                {
                    Email = "dev@email.com",
                    UserName = "dev@email.com",
                    FirstName = "Dev",
                    LastName = "Deol"
                }, "123456");

                user3 = userManager.FindByEmail("dev@email.com");
            }

            if (!userManager.IsInRole(user3.Id, "General"))
            {
                userManager.AddToRole(user3.Id, "General");
            }

            //Seed cars
            if (!db.Cars.Any())
            {
                db.Cars.Add(new Car
                {
                    ImageUrl = "http://media.ed.edmunds-media.com/honda/accord/2006/oem/2006_honda_accord_sedan_ex-v-6_rq_oem_1_500.jpg",
                    Make = "Honda",
                    Model = "Accord",
                    Year = 2006
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://photo.netcarshow.com/BMW-5-Series_2008_photo_05.jpg",
                    Make = "BMW",
                    Model = "5 Series",
                    Year = 2008
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Tesla",
                    Model = "Model S",
                    Year = 2015
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Honda",
                    Model = "Civic",
                    Year = 2001
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Toyota",
                    Model = "Prius",
                    Year = 2008
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Toyota",
                    Model = "Camry",
                    Year = 2010
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Ford",
                    Model = "Fiesta",
                    Year = 1999
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Chevrolet",
                    Model = "Camaro",
                    Year = 2013
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Dodge",
                    Model = "Rom",
                    Year = 2005
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Hyundai",
                    Model = "Sonata",
                    Year = 2009
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://photo.netcarshow.com/BMW-5-Series_2008_photo_05.jpg",
                    Make = "Ford",
                    Model = "Raptor",
                    Year = 2014
                });

                db.Cars.Add(new Car
                {
                    ImageUrl = "http://image.motortrend.com/f/wot/1403_shields_up_tesla_debuts_shield_to_protect_against_battery_punctures/61841299/2014-tesla-model-s-rear-three-quarters.jpg",
                    Make = "Fiat",
                    Model = "Jolly",
                    Year = 1958
                });
            }
            
            db.SaveChanges();

            //Seed user cars
            if (!db.UsersCars.Any())
            {
                db.UsersCars.Add(new UserCar
                {
                    Car = db.Cars.FirstOrDefault(c => c.Make == "Honda"),
                    User = user1
                });

                db.UsersCars.Add(new UserCar
                {
                    Car = db.Cars.FirstOrDefault(c => c.Make == "BMW"),
                    User = user2
                });

                db.UsersCars.Add(new UserCar
                {
                    Car = db.Cars.FirstOrDefault(c => c.Make == "Tesla"),
                    User = user3
                });
            }
        }
    }
}
